<template>
	<view>
		<u-navbar title="訂單詳情" placeholder autoBack>
			<!-- <view class="flexright" slot="right">
				<u-icon name="scan" color="#333" size="20"></u-icon>
			</view> -->
		</u-navbar>

		<view class="mainpadding" style="padding-bottom: 150rpx;">
			<view class="mainpadding_top ffffff line_b9" v-if="orderly==0 && appkey!=''&&is_sdkfp==0">
				<view class="text_weight margin_top color_san white_nowrap">發票類型<text style="color: #ff0000;">*</text>
				</view>
				<view class="margin_top flexbetween">
					<view :class="opentype==0?'flexleft select':'flexleft noSelect'" @click="opentype=0">
						<image class="vote1Img"
							:src="opentype==0?'@/static/images/mine/vote1_active.png':'@/static/images/mine/vote1.png'"
							mode=""></image>
						<view>紙本</view>
					</view>
					<view :class="opentype==1?'flexleft select':'flexleft noSelect'" @click="opentype=1;">
						<image class="vote2Img"
							:src="opentype==1?'@/static/images/mine/vote2_active.png':'@/static/images/mine/vote2.png'"
							mode=""></image>
						<view>載具</view>
					</view>
					<view :class="opentype==2?'flexleft select':'flexleft noSelect'" @click="opentype=2;">
						<image class="vote3Img"
							:src="opentype==2?'@/static/images/mine/vote3_active.png':'@/static/images/mine/vote3.png'"
							mode=""></image>
						<view>統編</view>
					</view>
					<view :class="opentype==3?'flexleft select':'flexleft noSelect'" @click="opentype=3;">
						<image class="vote4Img"
							:src="opentype==3?'@/static/images/mine/vote4_active.png':'@/static/images/mine/vote4.png'"
							mode=""></image>
						<view>捐贈</view>
					</view>
				</view>
				<view class="invoiceInput margin_top flexbetween" v-show="opentype!=0 && opentype!=-1">
					<input class="text_b" type="text" placeholder="請輸入手機載具" v-show="opentype==1" :disabled="istijiao"
						v-model="tbcode">
					<input class="text_b" type="text" placeholder="請輸入統一編碼" v-show="opentype==2" @blur="getTt()"
						:disabled="istijiao" v-model="tbcode">
					<input class="text_b" type="text" placeholder="請輸入捐贈碼" v-show="opentype==3" :disabled="istijiao"
						v-model="tbcode">
					<view class="" v-show="opentype==1 || opentype==3" @click="getCode()">
						<u-icon name="scan" color="#ffb800" size="28"></u-icon>
					</view>
				</view>
				<view class="invoiceInput margin_top" v-show="opentype==2">
					<input class="text_b" type="text" placeholder="請輸入發票抬頭" v-model="tbmc" :disabled="istijiao">
				</view>
			</view>
			<view class="mainpadding margin_top ffffff radius">
				<view class="flexbetween">
					<view class="titletext bold hei_text">商品</view>
					<!-- <view class="huang_text main_size bold flexright" @click="checkservice">
						<view class="margin_right1">請選擇</view>
						<u-icon name="arrow-right" color="#ff8505" size="16"></u-icon>
					</view> -->
				</view>
				<view :class="index+1==list.data.length?'':'pdl'" v-for="(item,index) in list.data" :key="item.id">
					<view class="flexleft margin_top">
						<view class="listleftimg radius margin_right">
							<image class="listleftimg radius" :src="item.logoimage" mode=""></image>
						</view>
						<view class="justify_between_column" style="height: 157rpx;width: 100%;">
							<view class="hei_text main_size bold">{{item.goods_name}}</view>
							<view class="xiaohui margin_top1 flexbetween" v-if="item.wei || item.hei">
								<text>尺寸：{{item.wei}}*{{item.hei}}</text>
								<text>用量：{{item.formulacount}}{{item.size}}</text>
							</view>
							<view class="xiaolan margin_top1"
								v-if="item.spunatdata_text && item.spunatdata_text.length">規格：<text
									class="margin_right1" v-for="(value) in item.spunatdata_text"
									:key="value.spunatid">{{value.spunatname}}*{{value.num}}&nbsp;&nbsp;&nbsp;</text>
							</view>
							<view class="flexbetween margin_top1">
								<view class="main_size huang_text bold">${{item.d_price}}&nbsp;&nbsp;&nbsp;x{{item.num}}
								</view>
								<view class="xiaohuang">${{item.price}}</view>
							</view>
							<view class="flexright margin_top1" @click="removeGoods(item.id)">
								<u-icon name="trash" color="#333" size="16"></u-icon>
							</view>
						</view>
					</view>
				</view>
				<view class="pdl" v-for="item in dataAll.cdbjhsf_data_text" :key="item.id" v-if="item.price!=0 && item.num!=0">
					<view class="flexleft margin_top">
						<view class="listleftimg radius margin_right">
							<image class="listleftimg radius" src="@/static/logo.png" mode=""></image>
						</view>
						<view class="justify_between_column" style="height: 157rpx;width: 100%;">
							<view class="hei_text main_size bold">{{item.name}}</view>
							<view class="flexbetween">
								<view class="main_size huang_text bold">${{item.price}}&nbsp;&nbsp;&nbsp;x{{item.num}}
								</view>
								<view class="main_size huang_text bold">${{item.price*item.num}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>

			<!-- <view class="mainpadding ffffff radius">
				<view class="titletext bold hei_text">訂單信息</view>
				<view class="flexbetween margin_top">
					<view class="hui_text main_size">新增商品總金額</view>
					<view class="huang_text main_size">${{totalPrice}}</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="hui_text main_size">未提交商品金額</view>
					<view class="huang_text main_size">${{totalwtjPrice}}</view>
				</view>
			</view> -->
			<view class="posibottom mainpadding" style="bottom: 0;">
				<view class="flexbetween">
					<view class="leftAllprice flexleft mainpadding ffffff">
						<view class="hei_text main_size">合計：</view>
						<view class="huang_text main_size" v-text="'$'+totalPrice"></view>
					</view>
					<view class="flexleft">
						<view class="leftsubmit flexcenter mainpadding"  v-if="hy_type==5" @click="yutijiao">
							<view class="xiaobai main_size">提交</view>
						</view>
						<view class="rightsubmit flexcenter mainpadding" @click="showpop">
							<view class="hei_text main_size" v-if="orderly==0">收款</view>
							<view class="hei_text main_size" v-if="orderly==1">提交</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-popup :show="show" @close="show=false" round="30" mode="center" closeable>
			<view class="mainpadding" style="min-width: 750rpx;max-width: 750rpx;">
				<view class="flexleft margin_top">
					<view class="bold hei_text" style="font-size: 52rpx;">總計金額：</view>
					<view class="bold xiaohei" style="font-size: 52rpx;" v-text="totalPrice"></view>
				</view>
				<view class="flexleft margin_top">
					<view class="bold hei_text" style="font-size: 52rpx;">應找金額：</view>
					<view class="bold xiaohong" style="font-size: 52rpx;" v-text="zhaoling"></view>
				</view>
				<view class="margin_top sytbox">
					<input type="text" class="sytsrk" placeholder="實收金額" disabled="" v-model="zhifuje" />
				</view>
				<view class="flexbetween flex-wrap margin_top">
					<view class="itembox flexcenter" v-for="item in numList" :key="item" @click="numtap(item)">{{item}}
					</view>
					<view class="itembox delitem flexcenter" @click="delnum">DEL</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="payTypeClass xiaobai" style="background-color: #00a0e9;" @click="submit(4)">信用卡</view>
					<view class="payTypeClass xiaobai" style="background-color: #ec7301;" @click="submit(2)">多元支付</view>
					<view class="payTypeClass xiaobai" style="background-color: #db0f62;" @click="submit(1)">錢包點數</view>
					<view class="payTypeClass xiaobai" style="background-color: #a70182;" @click="submit(3)">現金支付</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shop_id: uni.getStorageSync("shop_id"),
				list: {
					data: []
				},
				totalPrice: 0, //订單总金额
				totalwtjPrice: 0,
				zhifuje: "",
				show: false,
				orderly: 0, //1预约订單过来的需要直接跳转到预约订單
				tbcode: "",
				tbmc: "",
				opentype: 0,
				type: 0, //1自取，2内用
				istijiao: false, //如果後臺返回的有開票信息就不要再修改了
				numList: [7, 8, 9, 4, 5, 6, 1, 2, 3, "00", 0],
				shoptable_id: 0,
				shoptable_name: "",
				ztNum: 0,
				dataAll: {},
				is_ny: 0,
				is_zq: 0,
				appkey: "",
				is_sdkfp: 0,
				posdc: 0, //是否为pos点餐？0否，1是
				issubmit:false,
				hy_type:1
			}
		},
		onLoad(options) {
			this.hy_type = uni.getStorageSync("hy_type")
			this.appkey = uni.getStorageSync("appkey")
			this.is_sdkfp = uni.getStorageSync("is_sdkfp")
			uni.$on("returnCode", data => {
				this.tbcode = data
			})
			uni.$on("poscheckTab", data => {
				this.shoptable_id = data.id
				this.shoptable_name = data.name
			})
			if (options.posdc) {
				this.posdc = options.posdc
			}
			this.orderly = options.orderly
			this.getztnum()
		},
		onUnload() {
			uni.$off("returnCode")
			uni.$off("poscheckTab")
		},
		onShow() {
			this.getList()
			this.getInfo()
		},
		computed: {
			zhaoling() {
				let price = Number(this.zhifuje) - Number(this.totalPrice)
				if (price >= 0) {
					return price
				} else {
					return 0
				}
			}
		},
		methods: {
			getInfo() {
				this.http.request("/api/shop/Shopdeail", "POST", {
					shop_id: uni.getStorageSync("shop_id"),
				}).then(res => {
					uni.setStorageSync("appkey", res.data.appkey)
					uni.setStorageSync("hy_type", res.data.hy_type)
					uni.setStorageSync("opentable_status", res.data.opentable_status)
					uni.setStorageSync("is_sdkfp", res.data.is_sdkfp)
					uni.setStorageSync("is_call_sdkfp", res.data.is_call_sdkfp)
					uni.setStorageSync("is_pos_sdkfp", res.data.is_pos_sdkfp)
					uni.setStorageSync("houtel_type",res.data.houtel_type)
					this.is_ny = res.data.is_ny
					this.is_zq = res.data.is_zq
				})
			},
			async getztnum() { //使用列表
				let res = await this.http.request('/api/shop/listtable', 'POST', {
					shop_id: this.shop_id, //店鋪id
				})
				if (res.code == 1) {
					this.ztNum = res.data.length
				} else {
					this.ztNum = 0
				}
			},
			checkTab() {
				this.type = 2
				if (this.ztNum == 0) {
					return false
				}
				if (Number(this.dataAll.desk) > 0) {
					return false
				}
				uni.navigateTo({
					url: "/pages/goodsOrder/poszt"
				})
			},
			getCode() {
				uni.navigateTo({
					url: "/pages/my/getcodetwm"
				})
			},
			getTt() {
				if (this.opentype != 2) {
					return false
				}
				this.http.request("/api/user/getTbmc", "GET", {
					tbcode: this.tbcode
				}).then(res => {
					if (res.code == 1) {
						this.tbmc = res.data.tbinfo.name
					}
				})
			},
			numtap(item) {
				if (Number(item) == 0 && this.zhifuje == "") {
					return false
				}
				this.zhifuje = this.zhifuje + item
			},
			delnum() {
				let str = this.zhifuje;
				let newStr = str.slice(0, -1);
				this.zhifuje = newStr
				// 输出: Hello Worl
			},
			async getList() { //获取订單详情的商品
				let res = await this.http.request('/api/Ordergoods/deailOrdergoods', 'POST', {
					id: uni.getStorageSync("order_goods_id")
				})
				if (res.code == 1) {
					res.data.goods_data.forEach(item => {
						item.logoimage = this.imgUrlUser + item.logoimage
					})
					if (this.posdc == 1) {
						this.list.data = res.data.goods_data
					} else {
						this.list.data = res.data.goods_data.filter(item => {
							return item.is_zhui == 1 && item.is_zhuipay == 0
						})
					}
					this.dataAll = res.data
					this.totalPrice = 0
					this.totalwtjPrice = 0
					if (this.list.data.length) {
						this.list.data.forEach(item => {
							this.totalPrice = Number(this.totalPrice) + Number(item.price)
							if (item.is_ti == 0) {
								this.totalwtjPrice = Number(this.totalwtjPrice) + Number(item.price)
							}
						})
					}
					if (this.dataAll.cdbjhsf_data_text.length) {
						this.dataAll.cdbjhsf_data_text.forEach(item=>{
							this.totalPrice = Number(this.totalPrice) + Number(item.total_price)
							this.totalwtjPrice = Number(this.totalwtjPrice) + Number(item.total_price)
						})
					}
					this.opentype = res.data.opentype
					this.tbcode = res.data.tbcode
					this.tbmc = res.data.tbmc
					if (res.data.tbcode) {
						this.istijiao = true
					}
					console.log(this.list.data);
					// console.log(this.list.data)
				} else if (res.code == 0) {
					// this.list.data = []
					// this.http.toast(res.msg)
				} else {
					this.http.toast('系統處理中，請稍後再試');
				}
			},
			async removeGoods(id) { //刪除商品
				let res = await this.http.request('/api/ordergoods/delorderGoodslist', 'POST', {
					id
				})
				if (res.code == 1) {
					this.http.toast("刪除成功")
					this.getList()

				} else if (res.code == 0) {
					this.http.toast(res.msg)
				} else {
					this.http.toast('系統處理中，請稍後再試');
				}
			},
			checkservice() {
				// 選擇服務
				uni.navigateTo({
					url: "/pages/callOutOrder/xuanzefw"
				})
			},
			showpop() {
				if (this.orderly == 0) {
					if (this.appkey != "") {
						if (this.opentype == 1 && !this.tbcode) {
							this.http.toast("請輸入手機載具")
							return false
						}
						if (this.opentype == 2 && !this.tbcode) {
							this.http.toast("請輸入統一編碼")
							return false
						}
						if (this.opentype == 2 && !this.tbmc) {
							this.http.toast("請輸入發票擡頭")
							return false
						}
						if (this.opentype == 3 && !this.tbcode) {
							this.http.toast("請輸入捐贈碼")
							return false
						}
					}
					if (!this.list.data.length) {
						this.http.toast("請先選擇商品")
						return false
					}
					this.show = true
				} else {
					this.submit()
				}
			},
			yutijiao(){
				if (this.issubmit) return false
				let arr = []
				this.list.data.forEach(item => {
					if (item.is_ti == 0) {
						arr.push(item.id)
					}
				})
				if (arr.length == 0) {
					this.http.toast("暫未選擇新增商品，無法提交")
					return false
				}
				let data = {
					zhuimoney: this.totalwtjPrice,
					goods_list_ids: arr.join(),
					order_goods_id: uni.getStorageSync("order_goods_id"),
					ly: 2, //如果是预约订單提交的时候,来源传2其他情况传1
					order_attr:1,
				}
				this.issubmit = true
				this.http.request('/api/ordergoods/zhuiorderGoods', 'POST', data).then(res=>{
					this.issubmit = false
					if(res.code==1){
						this.http.toast("提交成功")
						uni.reLaunch({
							url: "/pages/index/index"
						})
					}else{
						this.http.toast(res.msg)
					}
				})
			},
			async submit(val) { //提交
				if (this.issubmit) return false
				let arr = []
				this.list.data.forEach(item => {
					if (item.is_ti == 0) {
						arr.push(item.id)
					}
				})
				if (arr.length == 0) {
					this.http.toast("暫未選擇新增商品，無法提交")
					return false
				}
				let data = {
					zhuimoney: this.totalwtjPrice,
					goods_list_ids: arr.join(),
					order_goods_id: uni.getStorageSync("order_goods_id"),
					ly: this.orderly == 1 ? 2 : 1 //如果是预约订單提交的时候,来源传2其他情况传1
				}
				if (this.orderly == 0) {
					this.$set(data, "opentype", this.appkey ? this.opentype : -1)
					this.$set(data, "tbcode", this.appkey ? this.tbcode : "")
					this.$set(data, "tbmc", this.appkey ? this.tbmc : "")
					this.$set(data, "paytype", val)
				}
				if (this.posdc == 1) {
					this.$set(data, "order_attr", 1)
				}
				this.issubmit = true
				let res = await this.http.request('/api/ordergoods/zhuiorderGoods', 'POST', data)
				if (res.code == 1) {
					if (val == 1) { //点数支付走这里
						uni.navigateTo({
							url: "/pages/dianshupay/dianshupay?type=1&id=" + uni.getStorageSync(
								"order_goods_id")
						})
					}else{
						this.http.toast("提交成功")
						uni.reLaunch({
							url: "/pages/index/index"
						})
					}
				} else {
					this.issubmit = false
					this.http.toast(res.msg)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.paddinglr {
		padding: 0 30rpx;
	}

	.header {
		padding: 6rpx;
		box-sizing: border-box;
		background: #F1F1F1;
		border-radius: 72rpx 72rpx 72rpx 72rpx;
		font-size: 30rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;

		view {
			width: 45%;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			background: #F1F1F1;
			border-radius: 72rpx 72rpx 72rpx 72rpx;
			font-size: 30rpx;
			font-family: PingFang SC-Heavy, PingFang SC;
			font-weight: 800;
			color: #666666;
		}

		.selects {
			width: 45%;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			background: #fff3e6;
			border-radius: 72rpx 72rpx 72rpx 72rpx;
			font-size: 30rpx;
			font-family: PingFang SC-Heavy, PingFang SC;
			font-weight: 800;
			color: #FF8505;
		}
	}

	.payTypeClass {
		text-align: center;
		font-size: 28rpx;
		font-weight: bold;
		padding: 40rpx 0;
		width: 150rpx;
	}

	.sytbox {
		border: 1rpx solid #000;
		border-radius: 10rpx;
		padding: 15rpx 30rpx;
	}

	.itembox {
		margin-top: 15rpx;
		width: 200rpx;
		height: 180rpx;
		border: 1rpx solid #000;
		border-radius: 10rpx;
		font-size: 60rpx;
		font-weight: bold;
		box-shadow: 0rpx 4rpx 16rpx 2rpx rgba(0, 0, 0, 0.16);
	}

	.delitem {
		background-color: #013447 !important;
		color: #fff !important;
	}

	.sytsrk {

		text-align: right;
		font-size: 40rpx;
		font-weight: bold;
	}


	.select {
		width: 154rpx;
		height: 69rpx;
		text-align: center;
		line-height: 69rpx;
		background: rgba(255, 133, 5, 0.1);
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 2rpx solid #FF8505;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FF8505;
		position: relative;
	}

	.select::after {
		content: '';
		width: 25rpx;
		height: 20rpx;
		position: absolute;
		right: 0;
		bottom: 0;
		background-image: url('/static/images/mine/invoice_select.png');
		background-size: 100% 100%;
	}

	.vote1Img {
		width: 26rpx;
		height: 26rpx;
		margin-left: 20rpx;
		margin-right: 10rpx;
	}

	.vote2Img {
		width: 23.5rpx;
		height: 23.5rpx;
		margin-left: 20rpx;
		margin-right: 10rpx;
	}

	.vote3Img {
		width: 29rpx;
		height: 29rpx;
		margin-left: 20rpx;
		margin-right: 10rpx;
	}

	.vote4Img {
		width: 27rpx;
		height: 27rpx;
		margin-left: 20rpx;
		margin-right: 10rpx;
	}

	.noSelect {
		width: 154rpx;
		height: 69rpx;
		text-align: center;
		line-height: 69rpx;
		background: rgba(102, 102, 102, .1);
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 2rpx solid rgba(102, 102, 102, .1);
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.invoiceInput {
		width: 100%;
		height: 91rpx;
		background: rgba(196, 196, 196, .1);
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		display: flex;
		align-items: center;
		padding-left: 20rpx;
	}
</style>